package com.fusion.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fusion.dao.UserRepository;
import com.fusion.domain.Dept;
import com.fusion.domain.QDept;
import com.fusion.domain.QUser;
import com.fusion.domain.User;
import com.fusion.page.PageInfo;
import com.querydsl.core.QueryResults;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;

@RestController
public class UserController {

	@Autowired
	private JPAQueryFactory queryFactory;
	@Autowired
	private UserRepository userRepository;

	/**
	 * 测试--查询全部
	 * @return
	 */
	@GetMapping("test")
	public Object test() {
		QUser user = QUser.user;
		return queryFactory.selectFrom(user).fetch();
	}

	/**
	 * 多条件查询
	 * @return
	 */
	@GetMapping("test1")
	public Object test1() {
		QUser user = QUser.user;
		QDept dept = QDept.dept;
		List<Tuple> fetch = queryFactory.select(user, dept).from(user, dept)
				.where(user.Id.eq(dept.userId).and(dept.id.isNotNull())).fetch();
		if (fetch != null && fetch.size() > 0) {
			for (Tuple tuple : fetch) {
				User user2 = tuple.get(0, User.class);
				Dept dept2 = tuple.get(1, Dept.class);
				System.out.println(user2);
				System.out.println(dept2);
			}
		}
		return null;
	}

	/**
	 * 子查询
	 * @return
	 */
	@GetMapping("test2")
	public Object test2() {
		QUser user = QUser.user;
		QDept dept = QDept.dept;
		JPQLQuery<Long> whereChild = JPAExpressions.select(dept.id).from(dept).where(dept.id.isNotNull());
		List<User> fetch = queryFactory.select(user).from(user).where(user.Id.in(whereChild)).fetch();
		return fetch;
	}

	/**
	 * 保存
	 * @return
	 */
	@GetMapping("test3")
	public Object test3() {
		User user = new User();
		user.setId(new Date().getTime());
		user.setUsername(UUID.randomUUID().toString());
		User save = userRepository.save(user);
		return save;
	}

	/**
	 * 更新-原生
	 * @return
	 */
	@GetMapping("test4")
	public Object test4() {
		// 为空的字段会修改
		User user = new User();
		user.setId(1L);
		user.setUsername(UUID.randomUUID().toString());
		User save = userRepository.save(user);
		return save;
	}

	/**
	 * 更新
	 * @return
	 */
	@Transactional
	@GetMapping("test5")
	public Object test5() {
		//执行更新和删除操作需要有事务注解
		QUser quser = QUser.user;
		long execute = queryFactory.update(quser).set(quser.username, UUID.randomUUID().toString())
				.where(quser.Id.eq(1L)).execute();
		return execute;
	}
	
	/**
	 * 删除
	 * @return
	 */
	@Transactional
	@GetMapping("test6")
	public Object test6() {
		QUser quser = QUser.user;
		long execute = queryFactory.delete(quser).where(quser.Id.isNull()).execute();
		return execute;
	}
	
	/**
	 * 统计数量
	 * @return
	 */
	@Transactional
	@GetMapping("test7")
	public Object test7() {
		QUser quser = QUser.user;
		Long fetchOne = queryFactory.select(quser.Id.count()).from(quser).fetchOne();
		return fetchOne;
	}
	
	/**
	 * 分页
	 * @return
	 */
	@Transactional
	@GetMapping("test8")
	public Object test8() {
		PageInfo pageInfo = new PageInfo(1,10);
		QUser quser = QUser.user;
		QueryResults<User> fetchResults = queryFactory.select(quser).from(quser).offset(pageInfo.getCurrent())
				.limit(pageInfo.getSize()).fetchResults();
		return fetchResults;
	}

}
